<?php

namespace App\Http\Controllers\Supply;

use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Http\Controllers\Controller;
use App\Models\Admin\AdminUser;
use DB;

class BaseController extends Controller
{
    use AdminTrait;
    public $user_id;
    public $jwt_user_info;

    public function __construct()
    {
        if(is_object(auth('admin')->user()))
        {
            $this->jwt_user_info=auth('admin')->user();
            $this->user_id = $this->jwt_user_info->id;
        }
        else{
            if (!$this->user_id){
                $this->user_id = auth('admin')->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub'];
                $user_info=AdminUser::find($this->user_id);
                $this->jwt_user_info=$user_info;
            }
        }

        // 本地判断是否开启mysql查询日志
        if (env('APP_ENV', 'pro') == 'local' && env('DB_LOG', false)) {
            \Illuminate\Support\Facades\DB::connection()->enableQueryLog();
        }
    }

    //自定义提示信息
    public $vali_msg=[
        'required'=>':attribute不存在',
        'integer'=>':attribute不是整型',
        'min'=>':attribute最小值为:min',
        'max'=>':attribute最大值为:max',
        'in'=>'::attribute 必须为以下值: :values',
        'filled'=>'::attribute 不能传空值',
        'numeric'=>'::attribute 必须是数字',
        'array'=>'::attribute 必须是数组',
        'date'=>'::attribute 必须是日期格式',
        'digits_between'=>'::attribute长度必须在:min到:max之间',
        'old_pwd.digits_between'=>'原密码长度必须在:min到:max之间',
        'new_pwd.digits_between'=>'新密码长度必须在:min到:max之间',
        'new_pwd.different'=>'新密码必须和旧密码不同',
        'new_pwd.required'=>'新密码不能为空',
        'old_pwd.required'=>'旧密码不能为空',
        'image'=>'必须上传图片',
        'new_pwd.regex'=>'密码必须为字母、数字或以下符号-.',
        'string'=>'::attribute必须为字符型',
        'exists'=>'::attribute找不到',
        'email'=>'邮箱格式不对',
        'mobile.digits'=>'手机号格式不对',
    ];

    //自定义验证规则
    public $vali_rules=[
        'page' => 'bail|required|integer|min:1',
        'size' => 'required|integer|min:1',
    ];

    //页码
    public $page='';

    //条数
    public $size='';

    //获取起始位置
    public $skip='';

    //获取页码以及条数
    public function getPageAndSize($data) {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty($data['size']) ? $data['size'] : 10;
        $this->skip = ($this->page - 1) * $this->size;
    }
}
